package com.woniu.service;

import com.woniu.exception.SignException;
import com.woniu.outlet.mysql.dao.SignDao;
import com.woniu.outlet.mysql.po.Sign;
import com.woniu.outlet.mysql.po.SignExample;
import com.woniu.utils.AppUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SignService {

    @Autowired
    private SignDao signDao;

    public Sign hasSign(Integer userId) {
        SignExample example = new SignExample();
        example.or().andUserIdEqualTo(userId);
        List<Sign> signs = signDao.selectByExample(example);
        if (signs.isEmpty()) {
            throw new SignException("未查到相关信息");
        }
        return signs.get(0);
    }

    public void doSign(Integer userId) {

        SignExample example = new SignExample();
        example.or().andUserIdEqualTo(userId);
        List<Sign> signs = signDao.selectByExample(example);
        Sign sign = signs.get(0);
        log.debug("签到的记录为[{}]", sign);
        if (sign.getStatus() == 1) {//已签到
            throw new SignException("今日已签到，请勿重复签到！！！");
        }

        //修改签到天数,签到时间
        sign.setSignDays(sign.getSignDays() + 1);
        sign.setSignTime(AppUtils.getNowTimeStr());
        sign.setStatus(1);
        log.debug("更新签到的记录：[{}]", sign);
        signDao.updateByPrimaryKey(sign);
    }
}
